package app2;

import java.util.List;
import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;
/**
 * 持久层
 * @author Alex
 *
 */
public class BookDao1 {
	/**
	 * 增加书籍
	 */
	public void add(Book book) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 //事物开始（默认）
			 //读取BookMapper.xml映射文件的SQL语句
			 sqlSession.insert(Book.class.getName()+".add",book);
			 //事物提交
			 sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			//事物回滚
			sqlSession.rollback();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	/**
	 * 根据id查询书籍
	 */
	public Book findById(int id) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 //读取BookMapper.xml映射文件的SQL语句
			 return sqlSession.selectOne(Book.class.getName()+".findById",id);	  
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	/**
	 * 查询所有书籍
	 */
	public List<Book> findAll() throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 //读取BookMapper.xml映射文件的SQL语句
			 return sqlSession.selectList(Book.class.getName()+".findAll");		 
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	/**
	 * 更新书籍信息
	 */
	public void update(Book book)throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 //事物开始（默认）
			 //读取BookMapper.xml映射文件的SQL语句
			 sqlSession.update(Book.class.getName()+".update",book);
			 //事物提交
			 sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			//事物回滚
			sqlSession.rollback();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	/**
	 * 删除书籍
	 */
	public void delete(Book book)throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 //事物开始（默认）
			 //读取BookMapper.xml映射文件的SQL语句
			 sqlSession.update(Book.class.getName()+".delete",book);
			 //事物提交
			 sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			//事物回滚
			sqlSession.rollback();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	
	
	/**
	 * 测试
	 */
	public static void main(String[] args) throws Exception{
		//创建Test对象
		//BookDao1 dao=new BookDao1();
		
		//增加书籍
		/*dao.add(new Book(1,"语文",20D));
		dao.add(new Book(2,"数学",20D));
		dao.add(new Book(3,"英语",20D));
		dao.add(new Book(4,"物理",15D));
	    dao.add(new Book(5,"化学",15D));
		dao.add(new Book(6,"生物",15D));	*/
		
		//根据ID查询书籍
		/*Book book=dao.findById(4);
		System.out.print(book.getId()+":"+book.getName()+":"+book.getSal());*/
		
		//查询所有书籍
		/*List<Book> bookList=dao.findAll();
		for (Book book : bookList) {
			System.out.print(book.getId()+":"+book.getName()+":"+book.getSal());
			System.out.println();
		}*/
		
		//修改书籍
		/*Book book=dao.findById(4);
		book.setName("政治");
		dao.update(book);*/
		
		//删除书籍
		/*Book book=dao.findById(4);
		dao.delete(book);*/
	}
}
